<?php
// 导入基础类
require_once(dirname(__FILE__) . '/base.class.php');
class Comment extends Base
{
    function __construct()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        parent::__construct();
        
        if(empty($this->openid)&&$_GET['a']!='lists'){
            $output['errcode']=500;
            $output['msg']='缺少openid';
            die(json_encode($output));
        }
    }
    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    /*
        获取评论列表
    */
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];

        $where = ' where 1=1 ';
        // 获取查询参数
        if (isset($_GET['course_id'])) {
            $course_id = $_GET['course_id'];
            $where .= " and course_id={$course_id}";
        }
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        if (!isset($no_page)) {
            $limit = " limit {$start},{$page_num}";
        }
        $sql = "select * from course_comments $where order by id desc $limit";
        // $output['sql']=$sql;
        $list = $this->db->query($sql);
        if (!empty($list)) {
            // 获取总记录数
            $sql = "select count(id) as count from course_comments {$where}";
            $res = $this->db->get_one($sql);
            $output['data'] = ['list' => $list, 'total' => $res['count']];
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取评论失败';
        }
        echo json_encode($output);
    }


    /*
        添加评论
    */
    function add()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $data = $_POST;
            $data['openid']=$this->openid;
            $data['createtime'] = date('Y-m-d H:i:s');
            $query = arr2str($data);
            $sql = "insert into course_comments set $query";
            $res = $this->db->query($sql);
            if ($res) {
                // 将评论id返回
                $output['comment_id'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }

    /*
     删除评论
    */
    function del()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            // 前5条评论不能删除
            if ($id > 5) {
                $sql = "delete from course_comments where id={$id}";
                $res = $this->db->query($sql);
                if (!$res) {
                    $output['errcode'] = 3;
                    $output['msg'] = '删除失败';
                }
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }

    
    /*
        我的评论列表
    */
    function my_comments()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];

        $where = " where openid='{$this->openid}' ";
    
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        if (!isset($no_page)) {
            $limit = " limit {$start},{$page_num}";
        }
        $sql = "select * from course_comments $where order by id desc $limit";
        // $output['sql']=$sql;
        $list = $this->db->query($sql);
        if (!empty($list)) {
            // 获取总记录数
            $sql = "select count(id) as count from course_comments {$where}";
            $res = $this->db->get_one($sql);
            $output['data'] = ['list' => $list, 'total' => $res['count']];
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取评论失败';
        }
        echo json_encode($output);
    }

}
